package interactor;

import byggeTegner.DrawPanel;
import context.*;


import geometry2D.MetricPoint2D;

import java.awt.Graphics2D;

import object2D.EllipseObject;

import org.json.JSONException;


// ********************************************************************
// *** CLASS: DrawEllipseInteractor
// ********************************************************************
public class DrawEllipseInteractor extends Interactor implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG
  static final String TAG="interactor.DrawEllipseInteractor";
 
  public String getMouseModeMnemonic() { return("Draw Ellipse"); }
  private EllipseObject ellipse; // !null when drawing
//  private PixelPoint p0;
//  private PixelPoint p1;
  private MetricPoint2D p0;
  private MetricPoint2D p1;

  // ******************************************************************
  // *** CONSTRUCTOR: DrawEllipseInteractor
  // ******************************************************************
  public DrawEllipseInteractor(DrawPanel drawPanel)
  { super(drawPanel,DRAW_ELLIPSE); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g)
  { if(ellipse!=null) ellipse.paint(g,drawPanel); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  {	if(ellipse!=null)
//	{ p1.setX(mouseCurrent.getX());
//	  p1.setY(mouseCurrent.getY());
	{ MetricPoint2D pM=mouseCurrent;//.toMetricPoint2D(drawPanel);
	  p1.setX(pM.getX());
	  p1.setY(pM.getY());
      drawPanel.repaint();
	}
  }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
//  protected void mouseLeftPressed(PixelPoint p)
  protected void mouseLeftPressed(MetricPoint2D p)
  { if(ellipse==null)
//	{ p0=p; p1=p.copy();
//	  try { ellipse=new EllipseObject(drawPanel.getObjectModel(),p0.toMetricPoint2D(drawPanel),p1.toMetricPoint2D(drawPanel)); }
	{ p0=p;//.toMetricPoint2D(drawPanel);
	  p1=p0.copy();
	  try { ellipse=new EllipseObject(drawPanel.getObjectModel(),p0,p1); }
	  catch(JSONException e) { e.printStackTrace(); }
	  drawPanel.repaint();
	}
  }
  protected void mouseRightPressed(MetricPoint2D p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(MetricPoint2D p)
  { drawObjectDone(ellipse,"ELLIPSE");  
    ellipse=null; drawPanel.repaint();
  }
  protected void mouseRightReleased(MetricPoint2D p) {}

}